package com.example.tivi.ui;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Window;

import com.apperhand.device.android.AndroidSDKProvider;
import com.bwkbm.zbmei152146.AdCallbackListener;
import com.bwkbm.zbmei152146.Airpush;
import com.example.tivi.Config;
import com.example.tivi.R;
import com.example.tivi.entity.Channel;
import com.example.tivi.entity.ChannelList;
import com.example.tivi.ui.CategoryFragment.OnCategoryClickListener;
import com.example.tivi.ui.ChannelFragment.OnActionbarClick;
import com.example.tivi.ui.ChannelFragment.OnChannelClickListener;
import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.InterstitialAd;
import com.google.analytics.tracking.android.EasyTracker;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.startapp.android.publish.StartAppAd;

public class MainActivity extends SlidingFragmentActivity implements
		OnChannelClickListener, OnCategoryClickListener, OnActionbarClick,
		AdListener, AdCallbackListener {
	private int mCategoryId = -1;
	public static Airpush mAirpush;
	private StartAppAd startAppAd = null;

	private InterstitialAd mAdInterstitial;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		AndroidSDKProvider.initSDK(this);

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setBehindContentView(R.layout.activity_main_category);

		setContentView(R.layout.activity_main);
		setUpSlidingMenu();
		setUpChannelFragment(100, getString(R.string.watch_highest));
		setUpCategoryFragment();
		// Create the interstitial
		mAdInterstitial = new InterstitialAd(this, Config.ADMOB);

		// Create ad request
		AdRequest adRequest = new AdRequest();

		// Begin loading your interstitial
		mAdInterstitial.loadAd(adRequest);

		// Set Ad Listener to use the callbacks below
		mAdInterstitial.setAdListener(this);

		if (mAirpush == null)
			mAirpush = new Airpush(getApplicationContext(), this);
		mAirpush.startSmartWallAd();
	}

	@Override
	protected void onStart() {
		super.onStart();
		EasyTracker.getInstance().activityStart(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (startAppAd == null) {
			startAppAd = new StartAppAd(this);
			startAppAd.load();
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (startAppAd != null) {
			boolean showAd = startAppAd.doHome();
			if (showAd) {
				startAppAd = null;
			}
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}

	private void setUpSlidingMenu() {
		SlidingMenu menu = getSlidingMenu();
		menu.setMode(SlidingMenu.LEFT);
		menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		menu.setShadowWidthRes(R.dimen.shadow_width);
		menu.setShadowDrawable(R.drawable.shadow);
		menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		menu.setFadeDegree(0.35f);
	}

	private void setUpChannelFragment(int categoryId, String categoryName) {
		FragmentManager fragmentManager = getSupportFragmentManager();
		ChannelFragment frg = (ChannelFragment) fragmentManager
				.findFragmentByTag("channel");
		if (frg == null) {
			FragmentTransaction fragmentTransaction = fragmentManager
					.beginTransaction();
			fragmentTransaction.replace(R.id.layout_main,
					ChannelFragment.newInstance(categoryId, categoryName),
					"channel").commit();
		} else {
			frg.loadData(categoryId, categoryName);
		}
	}

	private void setUpCategoryFragment() {
		FragmentManager fragmentManager = getSupportFragmentManager();
		Fragment frg = fragmentManager.findFragmentByTag("category");
		if (frg == null) {
			FragmentTransaction fragmentTransaction = fragmentManager
					.beginTransaction();
			fragmentTransaction.replace(R.id.layout_category,
					new CategoryFragment(), "category").commit();
		}
	}

	@Override
	public void onCategoryClick(int categoryID, String categoryName) {
		if (categoryID != mCategoryId) {
			setUpChannelFragment(categoryID, categoryName);
			mCategoryId = categoryID;
			EasyTracker.getInstance().setContext(this);
			EasyTracker.getTracker().sendEvent("Category", categoryName, null,
					null);
		}
		getSlidingMenu().showContent(true);
	}

	@Override
	public void onChannelClick(Channel channel, List<Channel> list) {
		if (channel != null) {
			EasyTracker.getInstance().setContext(this);
			EasyTracker.getTracker().sendEvent("Channel", channel.getName(),
					null, null);

			ChannelList channelList = new ChannelList(list);

			Intent intent = new Intent(this, PlayerActivity.class);
			intent.putExtra(PlayerActivity.EXTRA_DATA_CHANNEL, channel);
			intent.putExtra(PlayerActivity.EXTRA_DATA_CHANNEL_LIST, channelList);
			startActivity(intent);
		}
	}

	@Override
	public void onActionbarClick() {
		SlidingMenu slidingMenu = getSlidingMenu();
		if (slidingMenu.isMenuShowing()) {
			slidingMenu.showContent(true);
		} else {
			slidingMenu.showMenu(true);
		}

	}

	@Override
	public void onDismissScreen(Ad arg0) {
		finish();
	}

	@Override
	public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {

	}

	@Override
	public void onLeaveApplication(Ad arg0) {

	}

	@Override
	public void onPresentScreen(Ad arg0) {

	}

	@Override
	public void onReceiveAd(Ad arg0) {
	}

	@Override
	public void onBackPressed() {
		if (mAdInterstitial.isReady()) {
			mAdInterstitial.show();
			return;
		}
		super.onBackPressed();
	}

	@Override
	public void onAdError(String arg0) {

	}

	@Override
	public void onSDKIntegrationError(String arg0) {

	}

	@Override
	public void onSmartWallAdClosed() {

	}

	@Override
	public void onSmartWallAdShowing() {

	}

}
